﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Configuration;

namespace FundSms.SmsLib
{
    public class RegexHelper
    {
        private static Regex ChinaMobileReg;

        private static Regex ChinaUnicomReg;

        private static Regex ChinaTelecomReg;

        private static String ChinaMobile;

        private static String ChinaUnicom;

        private static String ChinaTelecom;

        private static String ValidateMethod;

        static RegexHelper()
        {
            ValidateMethod = ConfigurationManager.AppSettings["ValidateMethod"];

            ChinaMobileReg = new Regex(ConfigurationManager.AppSettings["ChinaMobileReg"], RegexOptions.IgnoreCase | RegexOptions.Compiled);
            ChinaUnicomReg = new Regex(ConfigurationManager.AppSettings["ChinaUnicomReg"], RegexOptions.IgnoreCase | RegexOptions.Compiled);
            ChinaTelecomReg = new Regex(ConfigurationManager.AppSettings["ChinaTelecomReg"], RegexOptions.IgnoreCase | RegexOptions.Compiled);

            ChinaMobile = ConfigurationManager.AppSettings["ChinaMobile"];
            ChinaUnicom = ConfigurationManager.AppSettings["ChinaUnicom"];
            ChinaTelecom = ConfigurationManager.AppSettings["ChinaTelecom"];
        }

        /// <summary>
        /// 获得指定号码归属的类型
        /// </summary>
        /// <param name="filter"></param>
        /// <param name="mobileno"></param>
        /// <returns></returns>
        public static string GetMobileType(string mobileno)
        {
            if (ValidateMethod.ToUpper() == "REG")
            {
                if (ChinaMobileReg.IsMatch(mobileno)) { return "Mobile"; }

                if (ChinaUnicomReg.IsMatch(mobileno)) { return "Unicom"; }

                if (ChinaTelecomReg.IsMatch(mobileno)) { return "Telecom"; }

                return "Telecom";
            }
            else
            {
                if (string.IsNullOrEmpty(mobileno))
                {
                    return "Telecom";
                }

                string prefix = mobileno.Substring(0, 3);

                if (prefix == "134")
                {
                    prefix = mobileno.Substring(0, 4);
                }

                if (ChinaMobile.IndexOf(prefix) >= 0)
                {
                    return "Mobile";
                }

                if (ChinaUnicom.IndexOf(prefix) >= 0)
                {
                    return "Unicom";
                }

                if (!mobileno.StartsWith(ChinaTelecom))
                {
                    return "Telecom";
                }

                return "Telecom";
            }

        }
    }
}
